import { useEffect, useState } from 'react';
import { message } from 'antd';
import { currentUser } from '@/services/ant-design-pro/api';

interface CurrentUser {
  userAccount: string;
  avatarUrl: string;
  username: string;
  // 其他用户信息字段
}

export function useCurrentUser() {
  const [user, setUser] = useState<CurrentUser | null>(null);
  const [loading, setLoading] = useState<boolean>(true);

  useEffect(() => {
    const fetchUser = async () => {
      try {
        const response = await currentUser();
        if (response.code === 0) {
          setUser(response.data);
        } else {
          message.error('获取用户信息失败');
          console.error('获取用户信息失败:', response.message);
        }
      } catch (error) {
        message.error('获取用户信息出错');
        console.error('获取用户信息出错:', error);
      } finally {
        setLoading(false);
      }
    };

    fetchUser();
  }, []);

  return { user, loading };
}
